import storeLocal from 'store'
import * as types from '../mutation-types'
import {isArray} from '../../utils/index'

const USER_KEY = 'user';
const userDef = {
    refreshToken:"",
    refreshTokenExpire:"",
    token:"",
    tokenExpire:"",
    workerId:"",
};
let userGetLocalStorage = ()=>{
    var d = storeLocal.get(USER_KEY);
    if(d&&d.token){
        for(let key in d){
            userDef[key] = d[key]
        }
    }
};
userGetLocalStorage();
const state = {
  device: {
      isMenuShow: false,
      isMenuMini: false,
      isSettingShow: false,
      themeSkin: 'skin-blue-dark',
      popupNum: 0,
      minHeight: 0,
  },
  effect: {
    translate3d: true
  },
  user:userDef,
  router:[]
};

const mutations = {

    [types.PUT_ROUTER] (state, data) {
        state.router.push(data);
    },

    [types.POP_ROUTER] (state, data) {
        if(state.router.length>0){
            if(data>=0){
                state.router = state.router.slice(0,data);
            }
        }
    },

  [types.SET_USER] (state, data) {
      state.user = data;
      storeLocal.set(USER_KEY,data);
  },

  [types.SET_MESSAGES] (state, data) {
      if(isArray(data)){
          state.user.message_list = data;
      }else {
          if(state.user.message_list.length>=3){
              state.user.message_list.pop();
          }
          state.user.message_list.unshift(data);
      }
      storeLocal.set(USER_KEY,state.user);
  },

  [types.TOGGLE_DEVICE] (state, device) {
      if (device.hasOwnProperty('isMenuShow')) {
          state.device.isMenuShow = device.isMenuShow
      }
      if (device.hasOwnProperty('isMenuMini')) {
          state.device.isMenuMini = device.isMenuMini
      }
      if (device.hasOwnProperty('isSettingShow')) {
          state.device.isSettingShow = device.isSettingShow
      }
      if (device.hasOwnProperty('themeSkin')) {
          state.device.themeSkin = device.themeSkin
      }
      if (device.hasOwnProperty('minHeight')) {
          state.device.minHeight = device.minHeight
      }
  },

  [types.SWITCH_EFFECT] (state, effectItem) {
    for (let name in effectItem) {
      state.effect[name] = effectItem[name]
    }
  }
};

const actions = {

};

export default {
  state,
  mutations,

}
